﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataObject.DataAccess;
using DataObject.Database;
using System.Web.Security;

public partial class Pages_Agent_AgentItem : System.Web.UI.Page
{

    #region MyRegion
    public string AgentId { get; set; } 
    #endregion

    #region Events

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["AgentId"] != null)
        {
            AgentId = Request["AgentId"];
        }
        else AgentId = string.Empty;
        if (!IsPostBack)
        {
            LoadData();
        }
    } 
    #endregion

    void LoadData()
    {
        HashSet<string> hs = new HashSet<string>();
        hs.Add("Value ASC");
        List<ParameterEntity> lsP = ParameterDAO.CreateInstant().SelectListItem(null, "Country", String.Empty, String.Empty, null, true, hs);

        // Load Country
        ddlCountry.DataSource = lsP;
        ddlCountry.DataTextField = "Value";
        ddlCountry.DataValueField = "ParamID";
        ddlCountry.DataBind();

        // Load User
        //MembershipUserCollection userc = Membership.GetAllUsers();
        ddlUserName.DataSource = GuestDAO.CreateInstant().GetListaspnet_User();
        ddlUserName.DataTextField = "UserName";
        ddlUserName.DataValueField = "UserId";
        ddlUserName.DataBind();

        // Fill AgentID
        Guest item = GuestDAO.CreateInstant().GetAgentById(AgentId);
        if (item != null)
        {
            txtGuestID.Text = item.GuestID;
            txtAddressLine1.Text = item.AddressLine1;
            txtAddressLine2.Text = item.AddressLine2;
            txtCity.Text = item.City;
            txtCompanyName.Text = item.CompanyName;
            txtEmail.Text = item.Email;
            txtFax.Text = item.Fax;
            txtFirstName.Text = item.FirstName;
            txtWebsite.Text = item.Website;
            txtNote.Text = item.Note;
            txtLastName.Text = item.Lastname;
            txtTel.Text = item.Tel;
            txtZipCode.Text = item.ZipCode;
            ddlCountry.SelectedValue = item.CountryID.ToString(CultureInfo.InvariantCulture);
            if (item.UserId != null) ddlUserName.SelectedValue = item.UserId.Value.ToString();
            ddlTitle.SelectedValue = item.Title;
            cbActive.Checked = item.Active;
            IList<AgentPricesEntity> lsAgent = AgentPricesDAO.CreateInstant().GetPrices(item.GuestID);
            grv.DataSource = lsAgent;
            grv.DataBind();
        }
    }
    private Guest GetGuestEntity()
    {
        Guest item = GuestDAO.CreateInstant().GetGuestById(AgentId);
        if (item == null)
        {
            item = new Guest();
            item.GuestID = txtGuestID.Text;
            item.GuestType = 0;
        }
        item.Active = cbActive.Checked;
        item.AddressLine1 = txtAddressLine1.Text;
        item.AddressLine2 = txtAddressLine2.Text;
        item.City = txtCity.Text;
        item.CompanyName = txtCompanyName.Text;
        item.CountryID = int.Parse(ddlCountry.SelectedValue);
        item.Email = txtEmail.Text;
        item.Fax = txtFax.Text;
        item.FirstName = txtFirstName.Text;
        item.Lastname = txtLastName.Text;
        item.Tel = txtTel.Text;
        item.Website = txtWebsite.Text;
        item.Note = txtNote.Text;
        item.ZipCode = txtZipCode.Text;
        if (!ddlUserName.SelectedValue.Equals(Guid.Empty.ToString()))
            item.UserId = new Guid(ddlUserName.SelectedValue);
        else item.UserId = Guid.Empty;
        item.Title = ddlTitle.SelectedValue;
        return item;
    }
    public bool checkagentidexist()
    {
        Guest _GuestEntity = GuestDAO.CreateInstant().GetGuestById(txtGuestID.Text);
        if (_GuestEntity != null)
            return true;
        return false;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (checkagentidexist() && AgentId.Equals(string.Empty))
        {
            lblAlert.Visible = true;
        }
        else
        {
            Guest item = GetGuestEntity();

            // Tempo to not process the Agent Price
            if (!AgentId.Equals(string.Empty))
            {
                GuestDAO.CreateInstant().Update(item);
                List<AgentPricesEntity> ls = GetListAgentPricesEntity();
                if (ls != null && ls.Count > 0)
                    foreach (AgentPricesEntity _AgentPricesEntity in ls)
                        AgentPricesDAO.CreateInstant().UpdatePrice(_AgentPricesEntity);
            }
            else
            {
                GuestDAO.CreateInstant().Add(item);
                List<AgentPricesEntity> ls = new List<AgentPricesEntity>();

                ls.Add(GetAgentPricesEntity(item.GuestID, "charter", null, null, "TJ", 0));
                AgentPricesDAO.CreateInstant().InsertPrices(ls);
            }
            Response.Redirect("AgentItem.aspx?AgentId=" + item.GuestID);
        }
    }
    //"cabin-price"
    List<AgentPricesEntity> GetListAgentPricesEntity()
    {
        List<AgentPricesEntity> ls = new List<AgentPricesEntity>();
        for (int i = 0; i < grv.Rows.Count; i++)
        {
            HiddenField hdf = (HiddenField)grv.Rows[i].FindControl("hdfAgentPricesId");
            TextBox txtPrice = (TextBox)grv.Rows[i].FindControl("txtPrice");
            AgentPricesEntity _AgentPricesEntity = AgentPricesDAO.CreateInstant().GetAgentPrice(new Guid(hdf.Value));
            _AgentPricesEntity.Price = long.Parse(txtPrice.Text);
            ls.Add(_AgentPricesEntity);
        }
        return ls;
    }
    AgentPricesEntity GetAgentPricesEntity(string GuestID, string PriceType, int? TypeOfCabin, int? RouteNumber, string Boat, int Price)
    {
        AgentPricesEntity _AgentPricesEntity = new AgentPricesEntity();
        _AgentPricesEntity.Id = Guid.NewGuid();
        _AgentPricesEntity.AgentID = GuestID;
        _AgentPricesEntity.PriceType = PriceType;
        _AgentPricesEntity.TypeOfCabin = TypeOfCabin;
        _AgentPricesEntity.Route = RouteNumber;
        _AgentPricesEntity.Boat = Boat;
        _AgentPricesEntity.Price = Price;

        return _AgentPricesEntity;
    }

}
